package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Customer;
import com.portal.logic.CustomerDAO;
import com.portal.logic.SerializationUtil;

public class EditCustomerScr extends JFrame {

	private JPanel contentPane;
	private JTextField txtRating;
	private JTextField txtName;
	private JTextField txtAge;
	private JTextField txtResidentialArea;
	private JTextField txtPhone;

	private static String selectedCustomer;
	private int customerId;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EditCustomerScr frame = new EditCustomerScr(selectedCustomer);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EditCustomerScr(String selectedCustomer) {
		setTitle("Edit Customer");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection());
		Customer customer = customerDAO.selectByName(selectedCustomer);
		customerId = customer.getId();
		JLabel label = new JLabel("Name");
		label.setBounds(59, 16, 56, 16);
		contentPane.add(label);
		
		txtRating = new JTextField();
		txtRating.setColumns(10);
		txtRating.setBounds(194, 129, 116, 22);
		txtRating.setText(customer.getRating()+"");
		contentPane.add(txtRating);
		
		JLabel label_1 = new JLabel("Age");
		label_1.setBounds(59, 45, 56, 16);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("Residential Area");
		label_2.setBounds(59, 74, 100, 16);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("Phone");
		label_3.setBounds(59, 103, 56, 16);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("Rating (From 1 to 10)");
		label_4.setBounds(59, 132, 130, 16);
		contentPane.add(label_4);
		
		txtName = new JTextField();
		txtName.setColumns(10);
		txtName.setBounds(194, 13, 116, 22);
		txtName.setText(customer.getName());
		contentPane.add(txtName);
		
		txtAge = new JTextField();
		txtAge.setColumns(10);
		txtAge.setBounds(194, 42, 116, 22);
		txtAge.setText(customer.getAge()+"");
		contentPane.add(txtAge);
		
		txtResidentialArea = new JTextField();
		txtResidentialArea.setColumns(10);
		txtResidentialArea.setBounds(194, 71, 116, 22);
		txtResidentialArea.setText(customer.getResidentialArea());
		contentPane.add(txtResidentialArea);
		
		txtPhone = new JTextField();
		txtPhone.setColumns(10);
		txtPhone.setBounds(194, 100, 116, 22);
		txtPhone.setText(customer.getPhone());
		contentPane.add(txtPhone);
		

		
		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(59, 189, 97, 25);
		contentPane.add(btnBack);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection());
				Customer customer= new Customer();
				customer.setId(customerId);
				String name = (String)txtName.getText();
				customer.setName(name);
				String ageStr = (String)txtAge.getText();
				customer.setAge(Integer.parseInt(ageStr));
				String residentialArea = (String)txtResidentialArea.getText();
				customer.setResidentialArea(residentialArea);
				String phone = (String)txtPhone.getText();
				customer.setPhone(phone);
				String rating = (String)txtRating.getText();
				customer.setRating(Integer.parseInt(rating));
				
				try {
					SerializationUtil.serialize(customer, "c:\\editCustomer.out");
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				
				customerDAO.update(); 
			    JOptionPane.showMessageDialog(null, "Customer was updated!");
				
			}
		});
		btnSubmit.setBounds(204, 164, 97, 25);
		contentPane.add(btnSubmit);
	}
}
